package top.lunarye.api.service.impl;

import top.lunarye.api.domain.SendTaskModel;
import top.lunarye.api.service.SendService;
import top.lunarye.api.domain.BatchSendRequest;
import top.lunarye.api.domain.SendRequest;
import top.lunarye.api.domain.SendResponse;
import org.springframework.stereotype.Service;
import top.lunarye.common.vo.BasicResultVO;
import top.lunarye.support.pipeline.ProcessContext;
import top.lunarye.support.pipeline.ProcessController;
import top.lunarye.support.pipeline.ProcessModel;

import javax.annotation.Resource;
import java.util.Collections;

/**
 * @author lunar
 * @since 2022-07-11
 */
@Service
public class SendServiceImpl implements SendService {

    @Resource
    private ProcessController processController;

    @Override
    public SendResponse send(SendRequest sendRequest) {
        SendTaskModel sendTaskModel = SendTaskModel.builder()
                .messageTemplateId(sendRequest.getMessageTemplateId())
                .messageParamList(Collections.singletonList(sendRequest.getMessageParam()))
                .build();

        ProcessContext context = ProcessContext.builder()
                .code(sendRequest.getCode())
                .processModel(sendTaskModel)
                .needBreak(false)
                .response(BasicResultVO.success())
                .build();

        ProcessContext process = processController.process(context);

        return new SendResponse(process.getResponse().getStatus(), process.getResponse().getMsg());
    }

    @Override
    public SendResponse batchSend(BatchSendRequest batchSendRequest) {
        SendTaskModel sendTaskModel = SendTaskModel.builder()
                .messageParamList(batchSendRequest.getMessageParamList())
                .messageTemplateId(batchSendRequest.getMessageTemplateId())
                .build();

        ProcessContext<ProcessModel> context = ProcessContext.builder()
                .code(batchSendRequest.getCode())
                .processModel(sendTaskModel)
                .needBreak(false)
                .response(BasicResultVO.success())
                .build();

        ProcessContext process = processController.process(context);

        return new SendResponse(process.getResponse().getStatus(), process.getResponse().getMsg());
    }
}
